package com.interview.a_多线程.c_停止线程.a_正常情况下_停止线程;

/**
 * 正常情况下停止线程
 * 通过 条件判断进行中断
 * <p>
 * run方法内没有 wait 或 sleep 时 停止线程
 */
public class StopThread implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThread());
        thread.start();
        //        执行一秒时中断
        Thread.sleep(500);
        thread.interrupt();
    }

    @Override
    public void run() {
        int count = 0;
        System.out.println("Thread.currentThread().isInterrupted()---> " + Thread.currentThread().isInterrupted());
        while (!Thread.currentThread().isInterrupted() && count <= Integer.MAX_VALUE / 2) {
            if (count % 10000 == 0) {
                System.out.println(count + "  是1万的倍数");
            }
            count++;
        }
        System.out.println("任务运行结束...  当前 interrupt=" + Thread.currentThread().isInterrupted());
    }


}
